package com.bigteddy.BankMark;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class PiActivity extends Activity {
	TextView text;
	TextProgressBar prog;
	Button but;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pi);
		text = (TextView) findViewById(R.id.piContent);
		but = (Button) findViewById(R.id.startPi);
		prog = (TextProgressBar) findViewById(R.id.pi_progressBar);
		prog.setTextSize(20);

		but.setOnClickListener(new AdapterView.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				but.setEnabled(false);
				EditText iterations = (EditText) findViewById(R.id.pi_it);
				new CalcPiTask().execute(iterations.getText().toString());
			}
		});
	}

	class CalcPiTask extends AsyncTask<String, Integer, String> {
		int NoOfDigits = 1000;
		int totalNoOfDigits = 1000;
		int Rows = 15;
		int Cols = 120;
		int CurrPos = 0;
		int Chunk = 5;
		int FontSize = 15;
		int SpaceLen = 2;
		String Space = " ";
		StringBuilder Output;
		boolean Running;
		boolean Slow;
		int Nines;
		int q;
		int p = -1;
		int len;
		int[] a;
		int b;
		int d;
		int e;
		int g;
		int h;
		int i;
		int base;
		int field;
		String FormatString;

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			prog.setProgress(values[0]);
			prog.setMax(this.totalNoOfDigits);
			prog.setText("Calculated " + values[0] + ". decimal place");
		}

		@Override
		protected void onPostExecute(String result) {
			text.setText(result);
			prog.setText("Done!");
			but.setEnabled(true);
		}

		@Override
		protected void onPreExecute() {
			System.gc();

			this.Output = new StringBuilder();
			this.Chunk = 5;
			this.Running = true;
			prog.setMax(this.totalNoOfDigits);
		}

		@Override
		protected String doInBackground(String... arg0) {
			InitRun(Integer.parseInt(arg0[0]));
			run();
			return Output.toString();
		}

		private void InitRun(int paramInt) {
			this.NoOfDigits = paramInt;
			this.totalNoOfDigits = paramInt;
			InitSlowRun();
		}

		private void InitSlowRun() {
			this.Output.append(this.Space);
			this.len = (10 * this.NoOfDigits + 3 + 1);
			this.Nines = 0;
			this.CurrPos = this.SpaceLen;
			this.p = -1;
			this.a = new int[this.len];
			for (int j = 0; j < this.len; j++)
				this.a[j] = 2;
		}

		public void runSlow() {
			long then = SystemClock.elapsedRealtime();
			while ((this.NoOfDigits >= 0) && (this.Running)) {
				this.q = 0;
				int j = this.len;
				while (true) {
					j--;
					if (j < 1)
						break;
					this.q = (int) (this.q + 10L * this.a[j]);
					this.a[j] = (this.q % (j + j + 1));
					this.q /= (j + j + 1);
					this.q *= j;
				}
				this.q = (int) (this.q + 10L * this.a[0]);
				this.a[0] = (this.q % 10);
				this.q /= 10;
				if (this.q == 9) {
					this.Nines += 1;
				} else {
					if (this.p >= 0)
						OutputDigit("" + (this.p + this.q / 10));
					if (this.NoOfDigits < this.Nines)
						this.Nines = this.NoOfDigits;
					this.NoOfDigits -= this.Nines + 1;
					while (--this.Nines >= 0)
						OutputDigit(this.q == 10 ? "0" : "9");
					this.Nines = 0;
					this.p = (this.q == 10 ? 0 : this.q);
				}
				publishProgress(this.totalNoOfDigits - this.NoOfDigits);
			}
			long elapsed = SystemClock.elapsedRealtime() - then;
			String old = Output.toString();
			Output.delete(0, Output.length());
			Output.append("The actual data you want:\n");
			Output.append("Elapsed Time: " + elapsed + "ms!" + "\n\nPi:\n"
					+ old);
		}

		private void OutputDigit(String paramString) {
			if (this.NoOfDigits == this.totalNoOfDigits - 2)
				this.Output.append(".");
			int j = this.CurrPos / (this.Chunk + this.SpaceLen)
					* (this.Chunk + this.SpaceLen);
			if (j + this.Chunk + this.SpaceLen > this.Cols) {
				this.Output.append(this.Space);
				this.CurrPos = this.SpaceLen;
			} else if (j == this.CurrPos) {
				this.Output.append(this.Space);
				this.CurrPos += this.SpaceLen;
			}
			this.CurrPos += 1;
			this.Output.append(paramString);
		}

		public void run() {
			runSlow();
		}

		int printChunk(int paramInt) {
			this.CurrPos += this.Chunk + this.SpaceLen;
			if (this.CurrPos > this.Cols) {
				this.Output.append("\n");
				this.CurrPos = (this.Chunk + this.SpaceLen);
			}
			StringBuffer localStringBuffer = new StringBuffer(this.FormatString);
			int j = String.valueOf(paramInt).length();
			localStringBuffer.insert(this.Chunk + this.SpaceLen - j, paramInt);
			localStringBuffer.setLength(this.Chunk + this.SpaceLen);
			this.Output.append(localStringBuffer.toString());
			return this.Chunk;
		}
	}
}
